home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Plus 1995 #2
/
Amiga Plus CD - 1995 - No. 2.iso
/
pd
/
grafik
/
mandelsquare-ham
/
readilbm.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-04-11
|
8KB
|
442 lines
/** Revision Header * Header built automatically - do not edit! *************
*
* (C) Copyright 1991 by Olaf `Olsen' Barthel, all rights reserved
*
* Name .....: ReadILBM.c
* Created ..: Monday 26-Aug-91 11:20
* Revision .: 1
*
* Date Author Comment
* ========= ======== ====================
* 26-Aug-91 Olsen Created this file!
*
***************************************************************************/
#define BUFFER_SIZE 16384
STATIC BYTE FileBuffer[BUFFER_SIZE],
*BufferPtr;
STATIC LONG BytesInBuffer = 0;
extern struct IntuitionBase *IntuitionBase;
LONG __regargs
FillBuffer(struct IFFHandle *Handle)
{
BytesInBuffer = ReadChunkBytes(Handle,FileBuffer,BUFFER_SIZE);
BufferPtr = FileBuffer;
return(BytesInBuffer);
}
STATIC LONG __regargs
SkipBuffer(struct IFFHandle *Handle,LONG Size)
{
LONG BytesSkipped = 0,Take;
while(Size)
{
if(!BytesInBuffer)
{
if(!FillBuffer(Handle))
return(BytesSkipped);
}
Take = Size > BytesInBuffer ? BytesInBuffer : Size;
BytesSkipped += Take;
Size -= Take;
BufferPtr += Take;
BytesInBuffer -= Take;
}
return(BytesSkipped);
}
STATIC LONG __regargs
ReadBuffer(struct IFFHandle *Handle,BYTE *Mem,LONG Size)
{
LONG BytesRead = 0,Take;
while(Size)
{
if(!BytesInBuffer)
{
if(!FillBuffer(Handle))
return(BytesRead);
}
Take = Size > BytesInBuffer ? BytesInBuffer : Size;
BytesRead += Take;
Size -= Take;
BytesInBuffer -= Take;
while(Take--)
*Mem++ = *BufferPtr++;
}
return(BytesRead);
}
STATIC BYTE
GetBuffer(struct IFFHandle *Handle)
{
if(!BytesInBuffer)
{
if(!FillBuffer(Handle))
return(-128);
}
BytesInBuffer--;
return(*BufferPtr++);
}
/* OpenImageFile(UBYTE *Name):
*
* Open an IFF-ILBM file with given name.
*/
struct IFFHandle * __regargs
OpenImageFile(UBYTE *Name)
{
struct IFFHandle *Handle;
if(Handle = AllocIFF())
{
if(Handle -> iff_Stream = Open(Name,MODE_OLDFILE))
{
InitIFFasDOS(Handle);
if(!OpenIFF(Handle,IFFF_READ))
return(Handle);
Close(Handle -> iff_Stream);
}
FreeIFF(Handle);
}
return(NULL);
}
/* CloseImageFile(struct IFFHandle *Handle):
*
* Close an IFF-ILBM file opened by OpenImageFile().
*/
VOID __regargs
CloseImageFile(struct IFFHandle *Handle)
{
CloseIFF(Handle);
Close(Handle -> iff_Stream);
FreeIFF(Handle);
}
/* ReadImageBody():
*
* Reads the image data contained in a standard 4 bit IFF-ILBM
* image file.
*/
BYTE __regargs
ReadImageBody(struct IFFHandle *Handle,struct BitMap *BitMap,struct BitMapHeader *BitMapHeader)
{
USHORT i,j,k;
UBYTE Value,Compr,Depth;
BYTE ChkVal;
LONG Height,Width,SoFar;
PLANEPTR Planes[9];
struct ContextNode *ContextNode;
BytesInBuffer = 0;
if(ContextNode = CurrentChunk(Handle))
{
Width = BitMap -> BytesPerRow;
Height = BitMap -> Rows;
Depth = BitMapHeader -> nPlanes;
Compr = BitMapHeader -> compression;
if(Compr > 1 || Depth < 1)
return(FALSE);
for(i = 0 ; i < Depth ; i++)
Planes[i] = BitMap -> Planes[i];
for(i = Depth ; i < 9 ; i++)
Planes[i] = NULL;
if(BitMapHeader -> masking == 1)
Depth++;
if(Compr == 0)
{
for(k = 0 ; k < Height ; k++)
{
for(j = 0 ; j < Depth ; j++)
{
if(Planes[j])
{
if(ReadBuffer(Handle,Planes[j],Width) != Width)
return(FALSE);
else
Planes[j] += Width;
}
else
{
if(SkipBuffer(Handle,Width) != Width)
return(FALSE);
}
}
}
}
if(Compr == 1)
{
for(k = 0 ; k < Height ; k++)
{
for(j = 0 ; j < Depth ; j++)
{
SoFar = Width;
if(Planes[j])
{
while(SoFar > 0)
{
if((ChkVal = GetBuffer(Handle)) != -128)
{
if(ChkVal > 0)
{
LONG Count = ChkVal + 1;
SoFar -= Count;
if(SoFar < 0)
return(FALSE);
else
{
if(ReadBuffer(Handle,Planes[j],Count) != Count)
return(FALSE);
Planes[j] += Count;
}
}
else
{
LONG Count = (-ChkVal) + 1;
SoFar -= Count;
if(SoFar < 0)
return(FALSE);
else
{
Value = (UBYTE)GetBuffer(Handle);
while(Count--)
*Planes[j]++ = Value;
}
}
}
}
}
else
{
while(SoFar > 0)
{
if((ChkVal = GetBuffer(Handle)) != -128)
{
if(ChkVal > 0)
{
LONG Count = ChkVal + 1;
SoFar -= Count;
if(SoFar < 0)
return(FALSE);
else
{
if(SkipBuffer(Handle,Count) != Count)
return(FALSE);
}
}
else
{
SoFar -= (-ChkVal) + 1;
if(SoFar < 0)
return(FALSE);
else
GetBuffer(Handle);
}
}
}
}
}
}
}
return(TRUE);
}
else
return(FALSE);
}
/* ReadImageHeader():
*
* Reads the header information, viewmodes and colours contained
* in an IFF-ILBM file.
*/
BYTE __regargs
ReadImageHeader(struct IFFHandle *Handle,ULONG *ViewModes,BYTE *NumCols,UWORD *Colours,struct BitMapHeader *BitMapHeader,struct MandelInfo *MandelInfo)
{
STATIC ULONG ImageProps[] =
{
'ILBM','BMHD',
'ILBM','CMAP',
'ILBM','CAMG',
'ILBM','MAND'
};
struct StoredProperty *Prop;
if(!PropChunks(Handle,&ImageProps[0],4))
{
if(!StopChunk(Handle,'ILBM','BODY'))
{
if(!ParseIFF(Handle,IFFPARSE_SCAN))
{
if(Prop = FindProp(Handle,'ILBM','BMHD'))
{
CopyMem(Prop -> sp_Data,BitMapHeader,sizeof(struct BitMapHeader));
if(BitMapHeader -> nPlanes > 0 && BitMapHeader -> nPlanes <= 8)
{
if(MandelInfo)
{
if(Prop = FindProp(Handle,'ILBM','MAND'))
{
MandelInfo -> Iterations = 32;
CopyMem(Prop -> sp_Data,MandelInfo,Prop -> sp_Size);
}
else
MandelInfo -> Iterations = 0;
}
if(ViewModes)
{
extern struct GfxBase *GfxBase;
*ViewModes = NULL;
if(BitMapHeader -> pageHeight > ((GfxBase -> DisplayFlags & PAL) ? 256 : 200))
*ViewModes |= LACE;
if(BitMapHeader -> pageWidth >= 640)
*ViewModes |= HIRES;
if(BitMapHeader -> nPlanes == 6)
*ViewModes |= HAM;
if(Prop = FindProp(Handle,'ILBM','CAMG'))
*ViewModes = *(ULONG *)Prop -> sp_Data;
*ViewModes &= ~(SPRITES | VP_HIDE | GENLOCK_AUDIO | GENLOCK_VIDEO);
if(ModeNotAvailable(*ViewModes))
*ViewModes &= 0xFFFF;
if(ModeNotAvailable(*ViewModes))
return(FALSE);
}
if(NumCols && Colours)
{
UBYTE *ColourArray;
WORD i,R,G,B;
if(!(Prop = FindProp(Handle,'ILBM','CMAP')))
return(FALSE);
ColourArray = (UBYTE *)Prop -> sp_Data;
*NumCols = Prop -> sp_Size / 3;
for(i = 0 ; i < *NumCols ; i++)
{
R = ColourArray[i * 3 + 0] >> 4;
G = ColourArray[i * 3 + 1] >> 4;
B = ColourArray[i * 3 + 2] >> 4;
Colours[i] = (R << 8) | (G << 4) | (B);
}
}
return(TRUE);
}
}
}
}
}
return(FALSE);
}
/* CreateBitMap(BYTE Depth,UWORD Width,UWORD Height):
*
* Creates and initializes a BitMap structure with
* given dimensions.
*/
struct BitMap * __regargs
CreateBitMap(BYTE Depth,UWORD Width,UWORD Height)
{
struct BitMap *BitMap;
BYTE i;
if(BitMap = (struct BitMap *)AllocVec(sizeof(struct BitMap),MEMF_PUBLIC|MEMF_CLEAR))
{
LONG PlaneSize;
InitBitMap(BitMap,Depth,Width,Height);
PlaneSize = BitMap -> BytesPerRow * BitMap -> Rows;
if(!(BitMap -> Planes[0] = AllocVec(PlaneSize * BitMap -> Depth,MEMF_CHIP|MEMF_PUBLIC|MEMF_CLEAR)))
{
FreeVec(BitMap);
return(NULL);
}
else
{
for(i = 1 ; i < Depth ; i++)
BitMap -> Planes[i] = &BitMap -> Planes[i - 1][PlaneSize];
}
}
return(BitMap);
}
/* DeleteBitMap(struct BitMap *BitMap):
*
* Deallocates any BitMap allocated by CreateBitMap().
*/
VOID __regargs
DeleteBitMap(struct BitMap *BitMap)
{
FreeVec(BitMap -> Planes[0]);
FreeVec(BitMap);
}